﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using YanZhiwei.DotNet3._5.Utilities.Base;

namespace YanZhiwei.DotNet3._5.Utilities.Common
{
    /// <summary>
    /// Lambda 帮助类
    /// </summary>
    public static class LambdaHelper
    {
        #region Distinct扩展
        /// <summary>
        /// Distinct扩展
        /// <para>eg:_source.Distinct(c => c.Name);</para>
        /// </summary>
        /// <param name="source">IEnumerable</param>
        /// <param name="keySelector">委托</param>
        /// <returns>IEnumerable</returns>
        public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
        {
            return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
        }
        /// <summary>
        /// Distinct扩展
        /// <para>eg:_source.Distinct(c => c.Name, StringComparer.InvariantCultureIgnoreCase);</para>
        /// </summary>
        /// <param name="source">IEnumerable</param>
        /// <param name="keySelector">委托</param>
        /// <param name="comparer">IEqualityComparer</param>
        /// <returns>IEnumerable</returns>
        public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector, IEqualityComparer<V> comparer)
        {
            return source.Distinct(new CommonEqualityComparer<T, V>(keySelector, comparer));
        }
        #endregion
        #region IF
        /// <summary>
        /// IF
        /// <para>eg:_source.AsQueryable().IF(true).ThenWhere(c => c.Age == 2);</para>
        /// </summary>
        /// <param name="source">IQueryable</param>
        /// <param name="condition">成立条件</param>
        /// <returns>ConditionalIQueryableSelector</returns>
        public static ConditionalIQueryableSelector<T> IF<T>(this IQueryable<T> source, bool condition)
        {
            return new ConditionalIQueryableSelector<T>(source, condition);
        }
        /// <summary>
        /// IF
        /// <para>eg:_source.AsEnumerable().IF(true).ThenWhere(c => c.Age == 2);/para>
        /// </summary>
        /// <param name="source">IEnumerable</param>
        /// <param name="condition">成立条件</param>
        /// <returns>ConditionalIEnumerableSelector</returns>
        public static ConditionalIEnumerableSelector<T> IF<T>(this IEnumerable<T> source, bool condition)
        {
            return new ConditionalIEnumerableSelector<T>(source, condition);
        }
        #endregion 
    }
}
